// 第一题：类似meeting rooms，输入是一个int[][] meetings, int start, int end, 每个数都是时间，13：00 =》 1300， 9：30 =》 18930， 看新的meeting 能不能安排到meetings
// ex: {[1300, 1500], [930, 1200],[830, 845]}, 新的meeting[820, 830], return true; [1450, 1500] return false;
package main

import (
	"fmt"
	"sort"
)

func schedukeMeeting(meetings [][2]int, newMeeting [2]int) bool {
	start, end := newMeeting[0], newMeeting[1]
	for _, meeting := range meetings {
		if (start >= meeting[0] && start < meeting[1]) ||
			(end > meeting[0] && end <= meeting[1]) ||
			(start <= meeting[0] && end >= meeting[1]) {
			return false
		}
	}
	return true
}

type Person struct {
	Name string
}

func findAvailableTimes(schedules [][2]int) [][2]int {
	schedules = mergeMeetings(schedules)
	availabilities := [][2]int{}
	if schedules[0][0] != 0 {
		availabilities = append(availabilities, [2]int{0, schedules[0][0]})
	}
	for i := 0; i < len(schedules)-1; i++ {
		availabilities = append(availabilities, [2]int{schedules[i][1], schedules[i+1][0]})
	}
	if schedules[len(schedules)-1][1] != 2400 {
		availabilities = append(availabilities, [2]int{availabilities[len(schedules)-1][1], 2400})
	}
	return availabilities
}

func mergeMeetings(schedules [][2]int) [][2]int {
	res := [][2]int{}
	sort.Slice(schedules, func(i, j int) bool {
		return schedules[i][0] <= schedules[j][0]
	})
	// fmt.Println(schedules)
	start := schedules[0][0]
	end := schedules[0][1]

	for i := 1; i < len(schedules); i++ {
		if schedules[i][0] >= start && schedules[i][0] <= end {
			end = Max(end, schedules[i][1])
		} else {
			res = append(res, [2]int{start, end})
			start = schedules[i][0]
			end = schedules[i][1]
		}
	}
	res = append(res, [2]int{start, end})
	return res
}

func Max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func main() {
	schedules := [][2]int{
		[2]int{1300, 1500},
		[2]int{930, 1200},
		[2]int{830, 845},
	}

	p1 := [][2]int{
		{1230, 1300},
		{845, 900},
		{1300, 1500},
	}

	p2 := [][2]int{
		{0, 844},
		{930, 1200},
		{1515, 1546},
		{1600, 2400},
	}

	p3 := [][2]int{

		{845, 915},
		{1515, 1545},
		{1235, 1245},
	}

	p4 := [][2]int{

		{1, 5},
		{844, 900},
		{1515, 1600},
	}

	// 	schedules1 = [p1_meetings, p2_meetings, p3_meetings]
	// schedules2 = [p1_meetings, p3_meetings]
	// schedules3 = [p2_meetings, p4_meetings]
	s0 := append(p1, p2...)
	s1 := append(s0, p3...)
	s2 := append(p1, p3...)
	s3 := append(p2, p4...)
	newM := [2]int{820, 830}
	fmt.Println(schedukeMeeting(schedules, newM))

	fmt.Println(findAvailableTimes(s1))
	fmt.Println(findAvailableTimes(s2))
	fmt.Println(findAvailableTimes(s3))
	// fmt.Println(mergeMeetings(s1))
	// fmt.Println(mergeMeetings(s2))
	// fmt.Println(mergeMeetings(s3))
	// fmt.Println("\n######## SearchInts not works in descending order  ######## ")
	// intSlice := []int{55, 54, 53, 52, 51, 50, 48, 36, 15, 5} // sorted slice in descending
	// x := 36
	// pos := sort.SearchInts(intSlice, x)
	// fmt.Printf("Found %d at index %d in %v\n", x, pos, intSlice)

	// fmt.Println("\n######## Search works in descending order  ########")
	// i := sort.Search(len(intSlice), func(i int) bool { return intSlice[i] <= x })
	// fmt.Printf("Found %d at index %d in %v\n", x, i, intSlice)
	// crowd := []Person{{"Zoey"}, {"Anna"}, {"Benni"}, {"Chris"}}

	// sort.Slice(crowd, func(i, j int) bool {
	// 	return crowd[i].Name <= crowd[j].Name
	// })

	// needle := "Benni"
	// idx := sort.Search(len(crowd), func(i int) bool {
	// 	return string(crowd[i].Name) >= needle
	// })
	// fmt.Println(crowd)
	// if crowd[idx].Name == needle {
	// 	fmt.Println("Found:", idx, crowd[idx])
	// } else {
	// 	fmt.Println("Found noting: ", idx)
	// }
}

// p1_meetings = [
//   (1230, 1300),
//   ( 845, 900),
//   (1300, 1500),
// ]

// p2_meetings = [
//   ( 0, 844),
//   ( 930, 1200),
//   (1515, 1546),
//   (1600, 2400),
// ]

// p3_meetings = [
//   ( 845, 915),
//   (1515, 1545),
//   (1235, 1245),
// ]

// p4_meetings = [
//   ( 1, 5),
//   (844, 900),
//   (1515, 1600)
// ]

// Expected output:

// findAvailableTimes(schedules1)
//  => [  844,  845 ],
//     [  915,  930 ],
//     [ 1200, 1230 ],
//     [ 1500, 1515 ],
//     [ 1546, 1600 ]

// findAvailableTimes(schedules2)
//  => [    0,  845 ],
//     [  915, 1230 ],
//     [ 1500, 1515 ],
//     [ 1545, 2400 ]

// findAvailableTimes(schedules3)
//     [  900,  930 ],
//     [ 1200, 1515 ]

// n = number of meetings
// s = number of schedules
